/*
实验二 运算符重载
一、	实验目的
1．	理解运算符重载的意义
2．	能将常见运算符重载为成员函数、友元函数
二、	实验题目
1．	定义一个复数类，重载运算符+和-，要求分别将运算符重载为成员函数和友元函数。（必做题）
2．	定义一个矩阵类，重载运算法+，使其实现矩阵的加法运算。（选做题）
*/

#include<iostream>
#include<string.h>
using namespace std;
class Complex
{
public:
	double real,imag;
	Complex(double r=0,double i=0):real(r),imag(i){}
	Complex operator+(const Complex &c);
	Complex operator-(const Complex &c);
	friend Complex operator+(double r,const Complex &c);
	friend Complex operator-(double r,const Complex &c);
};

Complex Complex::operator+(const Complex &c)
{
	return Complex(real+c.real,imag+c.imag);
}

Complex Complex::operator-(const Complex &c)
{
	return Complex(real-c.real,imag-c.imag);
}

Complex operator+(double r,const Complex &c)
{
	return Complex(c.real+r,c.imag);
}
Complex operator-(double r,const Complex &c)
{
	return Complex(c.real-r,c.imag);
}

int main()
{
	Complex a(0,0),b(0,0),c,d;
	cout<<"请输入a的实部与虚部：";
	cin>>a.real>>a.imag;
	cout<<"请输入b的实部与虚部：";
	cin>>b.real>>b.imag;
	c=a+b;
	d=a-b;
	cout<<c.real<<","<<c.imag<<endl;
	cout<<d.real<<","<<d.imag<<endl;
	system("pause"); 
	return 0;
}